///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Summit Contributors.  
// See: http://code.google.com/p/tyemill/wiki/SummitContributors
//
// The name and logo of Summit are trademarks of TYEMILL, LLC - All Rights 
// Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without 
// limitation the rights to use, copy, modify, merge, publish, distribute, 
// sublicense, and/or sell copies of the Software, and to permit persons to 
// whom the Software is furnished to do so, subject to the following 
// conditions:
//
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////

package summit.basecamp.wrappers
{
import summit.basecamp.constants.BasecampResource;
import summit.basecamp.constants.CommentableType;
import summit.basecamp.helpers.MilestoneHelper;
import summit.data.AccountToken;
import summit.net.BasecampLoader;
import summit.basecamp.wrappers.IStandardWrapper;

/**
 * Wrapper for Basecamp milestone operations.
 */
public class MilestoneWrapper extends AbstractWrapper implements
ICommentableWrapper, IListableWrapper, IStandardWrapper
{
	/** Constructor.
	 * @param token The <code>AccountToken</code> that is to be used in
	 * identifying the Basecamp account to which API calls are made.
	 * @param loader The <code>BasecampLoader</code> that is to be used 
	 * with all requested operations.
	 */
	public function MilestoneWrapper(token:AccountToken = null, 
	loader:BasecampLoader = null)
	{
		super(BasecampResource.MILESTONE, token, loader);
	}
	
	/** Returns the <code>CommentableType.MILESTONE</code>. **/
	public function get commentableType():String
	{
		return CommentableType.MILESTONE;
	}
	
	/**
	 * Marks a milestone as complete.
	 * 
	 * @param id The id of the milestone to be marked as complete.
	 */ 
	public function complete(id:String):void
	{
		loader.load(MilestoneHelper.complete(id));
	}
	
	/**
	 * Creates a new milestone.
	 * 
	 * <p>Typically, the XML provided is based on the template returned from
	 * a call to <code>prepare</code>.</p>
	 * 
	 * <p><code>create</code> on milestone can take up to 10 separate 
	 * milestones as an array. All milestones should be submitted for the
	 * same project.</p>
	 * 
	 * <p><code>title</code> and <code>deadline</code> are required by 
	 * Basecamp. Summit additionally requires <code>project-id</code>.</p>
	 * 
	 * @param content Represents a milestone.
	 */ 
	public function create(content:XML):void
	{
		loader.load(MilestoneHelper.create(content));	
	}
	
	/**
	 * Deletes a milestone.
	 * 
	 * @param id The id of the milestone to be deleted.
	 */ 
	public function destroy(id:String):void
	{
		loader.load(MilestoneHelper.destroy(id));
	}
	
	/**
	 * Gets an existing milestone from the Basecamp store.
	 * 
	 * <p>The XML returned is pre-filled with the existing values of the 
	 * identified milestone, and is ready to be resubmitted 
	 * via the <code>update</code> function.
	 * 
	 * <p>To use <code>edit</code>, the <code>projectId</code> property must
	 * be set (either directly or via the constructor.)</p>
	 * 
	 * @param id The id of the milestone to be retrieved.
	 * @param projectId The id of the project associated with the milestone.
	 */
	public function edit(id:String, projectId:String):void
	{
		loader.load(MilestoneHelper.edit(id, projectId));
	}
	
	/**
	 * Gets a list of milestones for a project.
	 * 
	 * @param id The id of the project from which milestones are to be listed.
	 * 
	 * @see summit.basecamp.constants.MilestoneFilter 
	 */ 
	public function list(projectId:String):void
	{
		loader.load(MilestoneHelper.list(projectId));
	}
	
	/**
	 * Gets a list of milestones scoped by the provided filter.
	 * 
	 * <p>Providing a filter will scope the list accordingly. Filter choices 
	 are: all, late, completed and upcoming.</p>
	 * 
	 * @param id  The id of the project from which milestones are to be listed.
	 * 
	 * @see summit.basecamp.constants.MilestoneFilter 
	 */
	public function listByFilter(projectId:String, filter:String = "all"):void
	{
		loader.load(MilestoneHelper.list(projectId, filter));
	}
	
	/**
	 * Retrieves a template for creating a new milestone.
	 * 
	 * <p>The template retrieved is used with <code>create</code> to 
	 * submit a new milestone.</p>
	 * 
	 * @param projectId The id of the project that is to be associated
	 * with the milestone when submitted via a call to <code>create</code>.
	 */ 
	public function prepare(projectId:String):void
	{
		loader.load(MilestoneHelper.prepare(projectId)); 
	}
	
	/**
	 * Reads a single Milestone.
	 * 
	 * <p>The <code>projectId</code> property must be set either from the 
	 * constructor of this class or through the property by the same name.</p>
	 * 
	 * @param id The id of the milestone to be read.
	 * @param projectId The id of the project from which milestones 
	 * are to be read. 
	 */
	public function read(id:String, projectId:String):void
	{
		loader.load(MilestoneHelper.read(id, projectId));	
	}
	
	/**
	 * Marks a milestone as uncomplete.
	 * 
	 * @param id The id of the milestone to be marked as uncomplete. 
	 */ 
	public function uncomplete(id:String):void
	{
		loader.load(MilestoneHelper.uncomplete(id));
	}
	
	/**
	 * Updates an existing milestone.
	 * 
	 * <p><code>content</code> submitted to <code>update</code> is typically
	 * retrieved from <code>edit</code>.</p>
	 * 
	 * @param content The updated XML content of an existing milestone.
	 */ 
	public function update(content:XML):void
	{
		loader.load(MilestoneHelper.update(content));
	}
}
}
//////////////////////////////////////////////////////// gmarius@tyemill.com //